package com.swdegao.api.callback;

import com.alibaba.fastjson2.JSONObject;
import com.swdegao.api.util.RocketMQUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.xml.bind.DatatypeConverter;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

import static ch.qos.logback.core.encoder.ByteArrayUtil.hexStringToByteArray;

@Slf4j
@RestController
@RequestMapping("/Api/GuoXinOneNETCallBackApi")
public class GuoXinOnenetCallBackApi {

    private static String token ="123qwe456rty789uio";//用户自定义token和OneNet第三方平台配置里的token一致
    private static String aeskey ="AbCdEfGhRjKlMnOpQrStUvWxYz";//aeskey和OneNet第三方平台配置里的token一致

    @Autowired
    private RocketMQUtils rocketMQUtils;

    private static String topic = "guoxinonenetTopic";
    private static String tag = "guoxinOneNETtag01";

    @PostMapping("/receive")
    public void callback(@RequestBody String body)throws IOException{
        System.out.println(body);
        log.info("data receive:  body String --- " +body);
        JSONObject json = JSONObject.parseObject(body);
        if (json == null) {
            log.error("推送数据为空");
        }else {
            String imei = JSONObject.parseObject(json.getString("msg")).getString("imei");
            rocketMQUtils.sendMessage(imei,topic,tag, json.toJSONString());
        }
    }



    //安全验证必须验证一次才可以使onenet的http数据推送成为生效模式
    @GetMapping( "/receive")
    public String check(@RequestParam(value = "msg") String msg,
                        @RequestParam(value = "nonce") String nonce,
                        @RequestParam(value = "signature") String signature) throws UnsupportedEncodingException {
        System.out.println(msg);
        System.out.println(nonce);
        System.out.println(signature);
       return msg;
    }


}
